<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :async="true"
            :title="popupTitle"
            width="1000px"
            @close="handleClose"
            @confirm="handleClose"
        >
            <div style="width:100%;height: 70vh;overflow-y: auto">
                <el-form ref="formRef" :model="formData" disabled label-width="130px" style="width: 600px"
                >
                    <!--                <el-form-item label="物联网卡号" prop="iccid">-->
                    <!--                    <el-input v-model="formData.iccid" clearable placeholder="请输入物联网卡号" />-->
                    <!--                </el-form-item>-->
                    <el-form-item label="IMEI" prop="imei">
                        <el-input v-model="formData.imei" clearable placeholder="请输入IMEI" />
                    </el-form-item>
                    <el-form-item label="设备名称" prop="name">
                        <el-input v-model="formData.name" clearable placeholder="请输入设备名称" />
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                    </el-form-item>
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="formData.address" clearable placeholder="请输入地址" />
                    </el-form-item>
                    <!--                <el-form-item label="地址经度" prop="lon">-->
                    <!--                    <el-input v-model="formData.lon" clearable placeholder="请输入地址经度" />-->
                    <!--                </el-form-item>-->
                    <!--                <el-form-item label="地址纬度" prop="lat">-->
                    <!--                    <el-input v-model="formData.lat" clearable placeholder="请输入地址纬度" />-->
                    <!--                </el-form-item>-->
                    <el-form-item label="设备图片" prop="image">
                        <el-image
                            :preview-src-list="[formData.image]"
                            :src="formData.image"
                            style="width: 100px; height: 100px">
                        </el-image>
                    </el-form-item>
                    <el-form-item label="电量" prop="electricity">
                        <el-input v-model="formData.electricity" clearable placeholder="请输入电量" />
                    </el-form-item>
                    <el-form-item label="油量" prop="oil_state">
                        <el-tag>{{ formData.oil_state == 1 ? '正常' : '缺油' }}</el-tag>
                    </el-form-item>
                    <el-form-item label="信号" prop="signal">
                        <el-input v-model="formData.signal" clearable placeholder="请输入信号" />
                    </el-form-item>
                    <el-form-item label="默认音量" prop="default_volume">
                        <el-input v-model="formData.default_volume" clearable placeholder="请输入默认音量" />
                    </el-form-item>
                    <el-form-item label="低音量开启时间" prop="low_pitch_start">
                        <el-input v-model="formData.low_pitch_start" clearable placeholder="请输入低音量开启时间" />
                    </el-form-item>
                    <el-form-item label="低音量结束时间" prop="low_pitch_end">
                        <el-input v-model="formData.low_pitch_end" clearable placeholder="请输入低音量结束时间" />
                    </el-form-item>
                    <el-form-item label="低音量" prop="bass_volume">
                        <el-input v-model="formData.bass_volume" clearable placeholder="请输入低音量" />
                    </el-form-item>
                    <el-form-item label="自动开炮" prop="automatic_firing">
                        <el-tag>{{ formData.automatic_firing == 1 ? '是' : '否' }}</el-tag>
                    </el-form-item>
                    <el-form-item label="低电量禁用阈值" prop="low_electricity">
                        <el-input v-model="formData.low_electricity" clearable placeholder="请输入低电量禁用阈值" />
                    </el-form-item>
                    <el-form-item label="二维码" prop="qr_code">
                        <el-image
                            :preview-src-list="[formData.qr_code]"
                            :src="formData.qr_code"
                            style="width: 100px; height: 100px">
                        </el-image>
                    </el-form-item>
                    <el-form-item label="是否禁用" prop="is_forbidden">
                        <el-tag>{{ formData.is_forbidden == 1 ? '是' : '否' }}</el-tag>
                    </el-form-item>
                    <el-form-item label="是否在线" prop="online">
                        <el-tag>{{ formData.online == 1 ? '是' : '否' }}</el-tag>
                    </el-form-item>
                    <el-form-item label="使用中" prop="in_use">
                        <el-tag>{{ formData.in_use == 1 ? '是' : '否' }}</el-tag>
                    </el-form-item>
                    <el-form-item label="设备编号" prop="code">
                        <el-input v-model="formData.code" clearable placeholder="请输入设备编号" />
                    </el-form-item>
                </el-form>
            </div>

        </popup>
    </div>
</template>

<script lang="ts" name="boomDeviceEdit" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiBoomDeviceDetail } from '@/api/boom_device'
import type { PropType } from 'vue'

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑设备表' : '新增设备表'
})

// 表单数据
const formData = reactive({
    id: '',
    agent_id: '',
    dealer_id: '',
    share_id: '',
    iccid: '',
    imei: '',
    name: '',
    remark: '',
    address: '',
    lon: '',
    lat: '',
    place_id: '',
    image: '',
    electricity: '',
    oil_state: '',
    signal: '',
    default_volume: '',
    low_pitch_start: '',
    low_pitch_end: '',
    bass_volume: '',
    pay_voice_id: '',
    end_voice_id: '',
    playing_voice_id: '',
    automatic_firing: '',
    low_electricity: '',
    qr_code: '',
    is_forbidden: '',
    online: '',
    in_use: '',
    code: ''
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiBoomDeviceDetail({
        id: row.id
    })
    setFormData(data)
}


//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
